package com.mw.mwreaderdemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class NT3H1101 extends Activity {

	private Spinner spCardMode;
	private Button btnOpenCard;
	private Button btnSelectSector;
	private Button btnReadPage;
	private Button btnReadPages;
	private Button btnWriteData;
	private Button btnCloseCard;
	private EditText etSector;
	private EditText etPage;
	private EditText etPageStart;
	private EditText etPageEnd;
	private EditText etWritePage;
	private EditText etPageData;
	LinearLayout llResult;
	
	String TAG = MainActivity.class.getSimpleName();
	Handler handler = new Handler();
	//  上一次点击的时间
	private long beforeClick = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_nt3h1101);
		
		etSector = (EditText)findViewById(R.id.etSector);
		etPage = (EditText)findViewById(R.id.etPage);
		etPageStart = (EditText)findViewById(R.id.etPageStart);
		etPageEnd = (EditText)findViewById(R.id.etPageEnd);
		etWritePage = (EditText)findViewById(R.id.etWritePage);
		etPageData = (EditText)findViewById(R.id.etPageData);
		
		spCardMode = (Spinner)findViewById(R.id.spCardMode);
		spCardMode.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
		
		btnOpenCard = (Button)findViewById(R.id.btnOpenCard);
		btnOpenCard.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(NT3H1101.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					String cardSnr = MainActivity.reader.openCard(spCardMode.getSelectedItemPosition());
					ShowResult(cardSnr);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					ShowResult(e.getMessage());
				}
			}
		});
		
		btnSelectSector = (Button)findViewById(R.id.btnSelectSector);
		btnSelectSector.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try{
					if (MainActivity.reader == null) {
						Toast.makeText(NT3H1101.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int sector = Integer.parseInt(etSector.getText().toString());
					MainActivity.reader.NT3H1101SelectSectorNo(sector);
					ShowResult("选择扇区成功");
				}catch (Exception e) {
					// TODO: handle exception
					ShowResult(e.getMessage());
				}
			}
		});
		
		btnReadPage = (Button)findViewById(R.id.btnReadPage);
		btnReadPage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try{
					if (MainActivity.reader == null) {
						Toast.makeText(NT3H1101.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int page = Integer.parseInt(etPage.getText().toString());
					String str = MainActivity.reader.NT3H1101ReadPageData(page);
					ShowResult(str);
				}catch (Exception e) {
					// TODO: handle exception
					ShowResult(e.getMessage());
				}
			}
		});

		btnReadPages = (Button)findViewById(R.id.btnReadPages);
		btnReadPages.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try{
					if (MainActivity.reader == null) {
						Toast.makeText(NT3H1101.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int pageStart = Integer.parseInt(etPageStart.getText().toString());
					int pageEnd = Integer.parseInt(etPageEnd.getText().toString());
					String str = MainActivity.reader.NT3H1101ReadPagesData(pageStart, pageEnd);
					ShowResult(str);
				}catch (Exception e) {
					// TODO: handle exception
					ShowResult(e.getMessage());
				}
			}
		});
		
		btnWriteData = (Button)findViewById(R.id.btnWriteData);
		btnWriteData.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try{
					if (MainActivity.reader == null) {
						Toast.makeText(NT3H1101.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					int page = Integer.parseInt(etWritePage.getText().toString());
					MainActivity.reader.NT3H1101WritePageData(page, etPageData.getText().toString());
					ShowResult("写页数据成功");
				}catch (Exception e) {
					// TODO: handle exception
					ShowResult(e.getMessage());
				}
			}
		});

		btnCloseCard = (Button)findViewById(R.id.btnCloseCard);
		btnCloseCard.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					if (MainActivity.reader == null) {
						Toast.makeText(NT3H1101.this, "设备未打开", Toast.LENGTH_SHORT).show();
						return;
					}
					MainActivity.reader.halt();
					ShowResult("卡片已关闭! ");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					ShowResult(e.getMessage());
				}
			}
		});
		
		llResult = (LinearLayout) findViewById(R.id.llResult);
		llResult.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					if (System.currentTimeMillis() - beforeClick > 500) {
						beforeClick = System.currentTimeMillis();
					} else {
						llResult.removeAllViews();
					}
				}
				return false;
			}
		});
	}
	
	private void ShowResult(final String str) {
		handler.post(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				TextView tv = new TextView(NT3H1101.this);
				tv.setText(str);
				llResult.addView(tv);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.nt3_h1101, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
